package me.gacl.websocket.example;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft;
import org.java_websocket.drafts.Draft_17;
import org.java_websocket.handshake.ServerHandshake;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.Map;

/**
 * WebSocket Client,,使用的是开源项目Java-WebSocket作为实现方案
 * https://github.com/TooTallNate/Java-WebSocket
 * Created by 孤傲苍狼 on 2016/2/18.
 */
public class ChatClient extends WebSocketClient{
    public ChatClient(URI serverURI) {
        super(serverURI);
    }

    public ChatClient(URI serverUri, Draft draft) {
        super(serverUri, draft);
    }

    public ChatClient(URI serverUri, Draft draft, Map<String, String> headers, int connecttimeout) {
        super(serverUri, draft, headers, connecttimeout);
    }

    @Override
    public void onOpen(ServerHandshake serverHandshake) {
        System.out.println("已经连接到WebSocket服务器【" + getURI() + "】:你可以向服务器发送聊天信息了");

    }

    @Override
    public void onMessage(String s) {
        System.out.println("获取到服务器发送的信息【" + s + "】");
    }

    @Override
    public void onClose(int i, String s, boolean b) {
        System.out.println("断开服务器连接【" + getURI() + "】");
    }

    @Override
    public void onError(Exception e) {
        System.out.println("WebSocket连接出错");
        e.printStackTrace();
    }

    public static void main(String[] args) throws Exception{
        //Draft d = new Draft_17();
        String clientName = "XDP";
        String protocol = "ws";
        String host = "localhost";
        int port = 8887;
        //服务器端WebSocket的连接地址
        //String serverLocation = protocol + "://" + host + ":" + port;
        String serverLocation = "ws://127.0.0.1:8080/websocket";
        ChatClient chatClient = null;
        boolean isConn = false;
        System.out.println( "你现在可以输入如下命令和WebSocket服务器端进行交互:" );
        System.out.println( "1.输入\"conn\"命令,连接上WebSocket服务器端" );
        System.out.println( "2.输入\"close\"命令,断开与WebSocket服务器端" );
        System.out.println( "3.输入\"exit\"命令,退出程序" );

        BufferedReader sysin = new BufferedReader(new InputStreamReader(System.in));
        while (true) {
            System.out.print( ">" );
            String stringIn = sysin.readLine();
            if (null != stringIn && stringIn.equalsIgnoreCase("conn")){
                if (isConn==false){
                    chatClient = new ChatClient(URI.create(serverLocation));
                    chatClient.connect();//连接到WebSocket服务器端
                    isConn = true;
                }
            }else if(null != stringIn && stringIn.equalsIgnoreCase("close")){
                if (isConn==true){
                    chatClient.close();
                    isConn = false;
                }

            }else if(null != stringIn && stringIn.equalsIgnoreCase("exit")){
                if (isConn==true){
                    chatClient.close();
                    isConn = false;
                }
                System.exit(0);
            }
            else{
                if (isConn==true){
                    //向WebSocket服务器端发送消息
                    if(null != stringIn && !stringIn.equals("")){
                        chatClient.send(clientName+":"+stringIn);
                    }else{
                        System.out.println( "请输入要发送的消息" );
                    }
                }else{
                    System.out.println( "WebSocket客户端还没有和WebSocket服务器端建立连接,消息无法发送到WebSocket服务器端,请输入conn命令连接WebSocket服务器端" );
                }

            }
        }

    }
}
